﻿var AjaxScript = {};
var Utility = AjaxScript.Utility = {
    CreateNameSpace: function (n, o) {
        var i, v;
        o = o || window;
        n = n.split('.');
        for (i = 0; i < n.length; i++) {
            v = n[i];
            if (!o[v]) {
                o[v] = {};
            }
            o = o[v];
        }
        return o;
    },
    CreateClass: function (fullName, obj) {
        var s = fullName.split('.');
        var className = s[s.length - 1];
        var ns = this.CreateNameSpace(fullName.substring(0, fullName.indexOf(className) - 1));
        ns[className] = function () { };
        for (var i in obj) {
            ns[className].prototype[i] = obj[i];
        }
    }
};
/*将javascript数据类型转换为json字符串 * @param 待转换对象,支持object,array,string,function,number,boolean,regexp * @return 返回json字符串 */
jQuery.extend({
    toJSON: function (object) {
        if (object == null) return 'null';
        var type = typeof object;
        if ('object' == type) {
            if (Array == object.constructor) type = 'array';
            else if (RegExp == object.constructor) type = 'regexp';
            else type = 'object';
        }
        switch (type) {
            case 'undefined':
            case 'unknown':
                return 'null';
                break;
            case 'function':
            case 'boolean':
            case 'regexp':
                return object.toString();
                break;
            case 'number':
                return isFinite(object) ? object.toString() : 'null';
                break;
            case 'string':
                return '"' + object.replace(/(\\|\")/g, "\\$1").replace(/\n|\r|\t/g, function () {
                    var a = arguments[0];
                    return (a == '\n') ? '\\n' : (a == '\r') ? '\\r' : (a == '\t') ? '\\t' : ""
                }) + '"';
                break;
            case 'object':
                if (object === null) return 'null';
                var results = [];
                for (var property in object) {
                    var value = jQuery.toJSON(object[property]);
                    if (value !== undefined) results.push(jQuery.toJSON(property) + ':' + value);
                }
                return '{' + results.join(',') + '}';
                break;
            case 'array':
                var results = [];
                for (var i = 0; i < object.length; i++) {
                    var value = jQuery.toJSON(object[i]);
                    if (value !== undefined) results.push(value);
                }
                return '[' + results.join(',') + ']';
                break;
        }
    }
});